package com.example.vae.Adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vae.Pojo.TieZi;
import com.example.vae.R;

import java.util.List;

//这个是初始化Find 页面的 Adapter
public class TieZiRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    private List<TieZi> tieZis;

    private LayoutInflater layoutInflater;
    public TieZiRecyclerViewAdapter(Context context){
        this.context = context;
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public TieZiRecyclerViewAdapter(Context context, List<TieZi> list){
        this.context=context;
        this.tieZis=list;
        layoutInflater=LayoutInflater.from(context);
    }

    //    初始化item布局
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

       View view=layoutInflater.inflate(R.layout.tiezi_item,parent,false);
        DisplayMetrics dm=context.getResources().getDisplayMetrics();
        int height=dm.heightPixels;

        MyViewHolder myViewHolder=new MyViewHolder(view);
        myViewHolder.adapter_gridView=new Adapter_GridView(context,tieZis.get(viewType).getImgs());
//        判断图片的数量》0 进行解析
        if(tieZis.get(viewType).getImgs().size()>0){
            switch (tieZis.get(viewType).getImgs().size()){
                case 1:
                    myViewHolder.grid.setNumColumns(1);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/2.7);
                    myViewHolder.grid.getLayoutParams().width=dm.widthPixels/2;

                    break;
                case 2:
                    myViewHolder.grid.setNumColumns(2);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/4);
//                    view.getLayoutParams().height=(int)(height/1.5);
                    break;
                case 3:
                    myViewHolder.grid.setNumColumns(3);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/6);
                    break;
                case 4:
                    myViewHolder.grid.setNumColumns(2);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/2);
//                    view.getLayoutParams().height=height;
                    break;
                case 5:
                    myViewHolder.grid.setNumColumns(3);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/2.7);
                    break;
                case 6:
                    myViewHolder.grid.setNumColumns(3);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/2.7);
                    break;
                default:
                    myViewHolder.grid.setNumColumns(3);
                    myViewHolder.grid.getLayoutParams().height=(int)(height/2);
                    break;
            }
            myViewHolder.grid.setVisibility(View.VISIBLE);
            myViewHolder.grid.setAdapter(myViewHolder.adapter_gridView);
        }else {
            myViewHolder.grid.setVisibility(View.GONE);
        }

        return myViewHolder;
    }

    //    处理每一个item 的地方,绑定数据
    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {

        MyViewHolder myViewHolder=(MyViewHolder)holder;
        myViewHolder.touxiang.setImageResource(tieZis.get(position).getTuxiang());
        myViewHolder.context.setText(tieZis.get(position).getContext());
        myViewHolder.date.setText(tieZis.get(position).getDate());
        myViewHolder.location.setText(tieZis.get(position).getLocation());
        myViewHolder.good.setText(String.valueOf(tieZis.get(position).getGood()));
        myViewHolder.mes.setText(String.valueOf(tieZis.get(position).getMes()));
        myViewHolder.name.setText(tieZis.get(position).getName());

    }

    //    显示数量，因为我的布局文件就是三个，所以我返回san
    @Override
    public int getItemCount() {
        return tieZis.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView touxiang;
        private TextView name,date,location,mes,good,context,type;

        private Adapter_GridView adapter_gridView;
        private GridView grid;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
            touxiang=itemView.findViewById(R.id.touxiang);
            date=itemView.findViewById(R.id.date);
            location=itemView.findViewById(R.id.location);
            mes=itemView.findViewById(R.id.mes);
            good=itemView.findViewById(R.id.good);
            context=itemView.findViewById(R.id.context);
            type=itemView.findViewById(R.id.type);
            grid=itemView.findViewById(R.id.gw);

        }
    }


}